/**
 * @widoc $.adaptElement
 * @namespace aux
 * @des 弹出位置计算
 * @type function
 * @param {object} elBCR 参照元素的 BCR
 * @param {int} viewW 视图范围的宽
 * @param {int} viewH 视图范围的高
 * @param {int} targetElWidth 弹出项的宽
 * @param {int} targetElHeight 弹出项的高
 * @param {string} positionStr 定位方向: 'p-p'，可能的值：top,left,right,bottom,center。
 * @param {boolean=} adjust 是否允许调整弹出方向
 * @param {object=} appendToEl 以参照元素计算定位，未定义则以窗口为参照(.overlay)
 * @return {Array} rObj 返回数组
 * @rtn {object} rObj[0] {top,bottom,left,right} 各字段对应的值为字符串。如：'100px' 或 ''
 * @rtn {string} rObj[1] 实际弹出方向。如：'top-right'
 */
$.adaptElement=function(elBCR, viewW, viewH, targetElWidth, targetElHeight, positionStr, adjust, appendToEl){
    var elTop=elBCR.top
        ,elLeft=elBCR.left
        ,elBot=elBCR.bottom
        ,elRight=elBCR.right;
    var positionStrParts = typeof positionStr=='string' ? positionStr.split('-') : [];
    var pos0= positionStrParts[0] || 'bottom'
        ,pos1= positionStrParts[1] || 'left';
    var cssObj={};
    if(adjust) {// 允许调整方向
        /**
         * 返回是否需要反向
         * @param nowS - 当前空间尺寸
         * @param otherS - 备选空间尺寸
         * @param targetS - 需要的空间尺寸
         */
        var shouldChange = function (nowS, otherS, targetS) {
            var nowLess = nowS < targetS,
                targetLess = otherS < targetS;
            return nowLess && (!targetLess || targetLess && otherS > nowS);//当前空间不足时，反向空间足够，或反向虽然不够，但比当前大，返回 true，即需要反向
        };
        // 确定 pos0，若当前空间不足且备选空间足够，或都不足但备选空间较大，则反向
        switch (pos0) {
            case 'left':
                shouldChange(elLeft, viewW - elRight, targetElWidth) && (pos0 = 'right');
                break;
            case 'right':
                shouldChange(viewW - elRight, elLeft, targetElWidth) && (pos0 = 'left');
                break;
            case 'top':
                shouldChange(elTop, viewH - elBot, targetElHeight) && (pos0 = 'bottom');
                break;
            default :
                pos0 = 'bottom';
                shouldChange(viewH - elBot, elTop, targetElHeight) && (pos0 = 'top');
        }
        // 确定 pos1
        switch (pos1) {
            case 'center':
                break;
            case 'top' :
                shouldChange(viewH - elTop, elBot, targetElHeight) && (pos1 = 'bottom');
                break;
            case 'bottom':
                shouldChange(elBot, viewH - elTop, targetElHeight) && (pos1 = 'top');
                break;
            case 'right':
                shouldChange(elRight, viewW - elLeft, targetElWidth) && (pos1 = 'left');
                break;
            default :
                pos1 = 'left';
                shouldChange(viewW - elLeft, elRight, targetElWidth) && (pos1 = 'right');
        }
    }
    if(appendToEl){
        // 一级位置已确定，通过返回方向由 class 名控制，不需计算
        // 二级方向位置确定，单向空间不足时，向右/下贴边
        switch(pos1){
            case 'center':
                if(['left', 'right'].indexOf(pos0) >= 0){
                    cssObj.top = Math.floor((elBot-elTop - targetElHeight)/2) + 'px';
                }
                else{
                    cssObj.left = Math.floor((elRight-elLeft - targetElWidth)/2) + 'px';
                }
                break;
            case 'top' :
                if(adjust != false && viewH - elTop < targetElHeight)
                    cssObj.top = viewH - elTop - targetElHeight + 'px';
                break;
            case 'bottom':
                if(adjust != false && elBot < targetElHeight)
                    cssObj.bottom = elBot - viewH + 'px';
                break;
            case 'right':
                if(adjust != false && elRight < targetElWidth)
                    cssObj.right = elRight - viewW + 'px';
                break;
            default:
                pos1 = 'left';
                if(adjust != false && viewW - elLeft < targetElWidth)
                    cssObj.left = viewW - elLeft - targetElWidth + 'px';
        }
    }else{
        // 根据参照元素的文档位置，计算弹出项的文档位置
        switch(pos0){
            case 'left':
                cssObj.left = elLeft - targetElWidth + 'px';
                break;
            case 'right':
                cssObj.left = elRight + 'px';
                break;
            case 'top':
                cssObj.top = elTop - targetElHeight + 'px';
                break;
            default :
                pos0 = 'bottom';
                cssObj.top = elBot + 'px';
        }
        // 二级方向位置确定，单向空间不足时，向右/下贴边
        switch(pos1){
            case 'center':
                if(['left', 'right'].indexOf(pos0) >= 0){
                    cssObj.top = elTop + Math.floor((elBot - elTop - targetElHeight)/2) + 'px';
                }
                else{
                    cssObj.left = elLeft + Math.floor((elRight - elLeft - targetElWidth)/2) + 'px';
                }
                break;
            case 'top':
                if(adjust && viewH - elTop < targetElHeight)
                    cssObj.top = viewH - targetElHeight + 'px';// 贴边
                else
                    cssObj.top = elTop + 'px';
                break;
            case 'bottom':
                if(adjust && elBot < targetElHeight)
                    cssObj.top = viewH - targetElHeight + 'px';// 贴边
                else
                    cssObj.top = elBot - targetElHeight + 'px';
                break;
            case 'right':
                if(adjust && elRight < targetElWidth)
                    cssObj.left = elLeft - elLeft + viewW - targetElWidth + 'px';
                else
                    cssObj.left = elRight - targetElWidth + 'px';
                break;
            default :
                pos1 = 'left';
                if(adjust && viewW - elLeft < targetElWidth)
                    cssObj.left = elLeft - elLeft + viewW - targetElWidth + 'px';
                else
                    cssObj.left = elLeft + 'px';
        }
    }
    return [{
        'top': cssObj.top ? cssObj.top : ''
        ,'bottom': cssObj.bottom ? cssObj.bottom : ''
        ,'left': cssObj.left ? cssObj.left : ''
        ,'right': cssObj.right ? cssObj.right : ''
    }, pos0 + '-' + pos1];
};